from views import page as pageBase

from api import api


class AccountMoveLine(object):
    # page = pageBase.accountMove
    def test(self,):
        # self.test_onchange2()
        self.test_function()

    def test_function(self):
        obj = api.odooenv['account.move']

        field_onchange_ref = 'account.view_move_form'
        # field_onchange_model = 'account.move'
        field_onchange_model = None
        field_onchange_field = 'line_ids'

        def get_field_onchange(field_onchange):
            if field_onchange:
                return field_onchange
            if not field_onchange_ref:
                return obj._onchange_spec()

            view_id = obj.env.ref(field_onchange_ref)
            ref_obj = field_onchange_model and obj.env[field_onchange_model] or obj
            view_info = ref_obj.fields_view_get(view_id)
            field_onchange = ref_obj.onchange_spec(view_info)
            if not field_onchange_field:
                return field_onchange

            return dict((k.split('.')[1], v) for k, v in field_onchange.items()
                        if len(k.split('.')) == 2 and k.split('.')[0] == field_onchange_field)

        # view_id = 534
        # res = .fields_view_get(view_id)
        # res = api.odooenv['account.move'].onchange_spec(res)

        field_onchange = get_field_onchange(field_onchange)
        res = field_onchange

        print(res)

    def test_onchange2(self):

        ids = [11]
        field = 'quantity'

        values_odoo = {
            "sequence": 10, "move_name": "/", "product_id": 4,
            "name": "可乐", "asset_category_id": False, "account_id": 143, "analytic_account_id": False, "analytic_tag_ids": [[6, False, []]], "quantity": 10, "product_uom_id": 1, "price_unit": 6, "discount": 0, "tax_ids": [[6, False, []]], "price_subtotal": 6, "price_total": 6, "partner_id": 9, "amount_currency": 0, "currency_id": False, "debit": 0, "credit": 6, "date": "2020-11-05", "date_maturity": False, "tax_line_id": False, "tax_repartition_line_id": False, "tag_ids": [[6, False, []]], "tax_base_amount": 0, "tax_exigible": True, "company_id": 2, "purchase_line_id": False, "company_currency_id": 7, "recompute_tax_line": False, "display_type": False, "is_rounding_line": False, "exclude_from_invoice_tab": False, "account_internal_type": "other", "always_set_currency_id": 7, "id": 11, "move_id": {"id": 6, "authorized_transaction_ids": [[6, False, []]], "state": "draft", "tax_lock_date_message": False, "type": "out_invoice", "invoice_payment_state": "not_paid", "invoice_filter_type_domain": "sale", "company_currency_id": 7, "commercial_partner_id": 9, "bank_partner_id": 7, "invoice_has_outstanding": False, "invoice_sent": False, "invoice_sequence_number_next_prefix": False, "invoice_sequence_number_next": False, "invoice_has_matching_suspense_amount": False, "has_reconciled_entries": False, "restrict_mode_hash_table": False, "name": "/", "partner_id": 9, "partner_shipping_id": 9, "ref": False, "invoice_vendor_bill_id": False, "purchase_id": False, "purchase_vendor_bill_id": False, "invoice_date": False, "date": "2020-11-05", "invoice_payment_term_id": False, "invoice_date_due": "2020-11-05", "journal_id": 9, "company_id": 2, "currency_id": 7, "invoice_line_ids": [[1, 11, {"quantity": 10}]], "amount_untaxed": 6, "amount_by_group": [], "amount_total": 6, "invoice_payments_widget": "False", "amount_residual": 6, "invoice_outstanding_credits_debits_widget": "false", "narration": False, "line_ids": [[4, 11, False], [4, 12, False]], "invoice_user_id": 2, "team_id": 1, "invoice_origin": False, "invoice_incoterm_id": False, "fiscal_position_id": False, "invoice_cash_rounding_id": False, "invoice_source_email": False, "invoice_payment_ref": False, "invoice_partner_bank_id": False, "campaign_id": False, "medium_id": False, "source_id": False, "auto_post": False, "reversed_entry_id": False, "to_check": False, "message_follower_ids": [[4, 36, False]], "activity_ids": [], "message_ids": [[4, 25, False]], "message_attachment_count": 0}


        }
        values_odoojs = {
            "date": "2020-11-05", "journal_id": 9,
            "move_id": {"id": 6, "display_name": "草稿发票 (* 6)", "ref": "",
                        "date": "2020-11-05", "name": "/", "partner_id": 9, "journal_id": 9,
                        "amount_total_signed": 6, "state": "draft",
                        "line_ids": [[4, 11, None], [4, 12, None]],
                        "auto_post": None, "company_id": 2, "currency_id": 7, "team_id": 1, "to_check": None,
                        "type": "out_invoice", "amount_by_group": [], "amount_residual": 6,
                        "amount_total": 6, "amount_untaxed": 6, "bank_partner_id": 7, "commercial_partner_id": 9, "company_currency_id": 7,
                        "partner_shipping_id": 9, "invoice_date": None, "invoice_partner_display_name": "ss222", "invoice_origin": None, "invoice_user_id": 2, "invoice_date_due": "2020-11-05",
                        "amount_residual_signed": 6, "invoice_payment_state": "not_paid", "invoice_incoterm_id": None, "invoice_sent": None, "invoice_line_ids": [[4, 11, None]], "invoice_filter_type_domain": "sale", "invoice_payment_ref": None, "invoice_payments_widget": "false", "invoice_sequence_number_next": None, "invoice_sequence_number_next_prefix": None, "invoice_has_outstanding": None,
                        "invoice_outstanding_credits_debits_widget": "false"}, "account_id": 143, "partner_id": 9, "ref": None, "name": "可乐", "analytic_account_id": None, "debit": 0, "credit": 6, "full_reconcile_id": None, "currency_id": None, "discount": 0, "display_type": None, "quantity": "10", "sequence": 10, "tax_exigible": True, "product_id": 4, "price_unit": "6", "price_subtotal": "6", "price_total": 6, "company_id": 2, "date_maturity": None, "product_uom_id": 1, "purchase_line_id": None, "display_name": "/ 可乐"

        }

        values = values_odoojs

        field_onchange = {


            "asset_category_id": "1",
            "account_id": "1",
            "partner_id": "",
            "name": "",
            "analytic_account_id": "1",
            "analytic_tag_ids": "1",
            "date_maturity": "",
            "amount_currency": "1",
            "currency_id": "1",
            "tax_ids": "1",
            "debit": "1",
            "credit": "1",
            "country_id": "",
            "tag_ids": "1",
            "payment_id": "1",
            "product_id": "1",
            "quantity": "1",
            "product_uom_id": "",
            "price_unit": "1",
            "discount": "1",
            "price_subtotal": "1",
            "price_total": "",
            "sequence": "",
            "move_name": "",
            "date": "",
            "tax_line_id": "1",
            "tax_group_id": "",
            "tax_repartition_line_id": "1",
            "tax_base_amount": "1",
            "tax_exigible": "",
            "company_id": "1",
            "purchase_line_id": "",
            "company_currency_id": "",
            "recompute_tax_line": "",
            "display_type": "",
            "is_rounding_line": "",
            "exclude_from_invoice_tab": "1",
            "account_internal_type": ""
        }

        field_onchange1 = {







            "asset_category_id": "1",
            "account_id": "1",
            "partner_id": "",
            "name": "",
            "analytic_account_id": "1",
            "analytic_tag_ids": "1",
            "date_maturity": "",
            "amount_currency": "1",
            "currency_id": "1",
            "tax_ids": "1",
            "debit": "1",
            "credit": "1",
            "country_id": "",
            "tag_ids": "1",
            "payment_id": "1",
            "product_id": "1",
            "quantity": "1",
            "product_uom_id": "",
            "price_unit": "1",
            "discount": "1",
            "price_subtotal": "1",
            "price_total": "",
            "sequence": "",
            "move_name": "",
            "date": "",
            "tax_line_id": "1",
            "tax_group_id": "",
            "tax_repartition_line_id": "1",
            "tax_base_amount": "1",
            "tax_exigible": "",
            "company_id": "1",
            "purchase_line_id": "",
            "company_currency_id": "",
            "recompute_tax_line": "",
            "display_type": "",
            "is_rounding_line": "",
            "exclude_from_invoice_tab": "1",
            "account_internal_type": ""
        }

        field_onchange2 = {
            'company_id': None,
            'parent_state': None,
            'name': None,
            'partner_id': None,
            'account_id': '1',
            'debit': '1',
            'credit': '1',
            'quantity': '1',
            'move_id': '1',
            'statement_id': None,
            'date': None,
            'date_maturity': None,
            'tax_line_id': None,
            'tax_ids': '1',
            'tax_audit': None,
            'full_reconcile_id': None,
            'matched_debit_ids': None,
            'matched_credit_ids': None,
            'currency_id': '1',
            'amount_currency': '1',
            'product_id': '1',
            'blocked': None,
            'analytic_account_id': '1',
            'analytic_tag_ids': '1',
            'analytic_line_ids': None
        }

        context = {
            "lang": "zh_CN", "tz": False, "uid": 6,
            "allowed_company_ids": [2],
            "default_currency_id": False,
            "default_partner_id": False,
            "default_type": "out_invoice",
            "journal_id": 9

        }

        res = api.odooenv['account.move.line'].onchange2(
            ids, values, field, field_onchange, context=context)

        print(res)

        # print('--value-')
        # for r, v in res['value'].items():
        #     print(r, v)
        # print('--domain-')
        # for r, v in res['domain'].items():
        #     print(r, v)
        # print('--fields-')
        # for r, v in res['fields'].items():
        #     if v['type'] == 'many2one':
        #         print(r, v)


class AccountMove(object):
    # page = pageBase.accountMove

    def test(self,):
        self.test_onchange2()

        # self.test_search()

    def test_onchange2(self):

        res = api.env['account.move'].onchange2([], {}, [])
        print('--value-')
        for r, v in res['value'].items():
            print(r, v)
        print('--domain-')
        for r, v in res['domain'].items():
            print(r, v)
        print('--fields-')
        for r, v in res['fields'].items():
            if v['type'] == 'many2one':
                print(r, v)

    def test_odoo_onchange2(self,):
        field_onchange = {
            "journal_id": "1",
            "partner_id": "",
            "fiscal_position_id": "",
            "invoice_payment_term_id": "1",
            "invoice_partner_bank_id": "",
            "id": "",
            "invoice_vendor_bill_id": "",
            "name": "",
            "company_id": "1",
        }

        context = {
            # 'default_company_id': 1,
            # 'default_product_uom_category_id': 1
        }
        res = api.odooenv['account.move'].onchange2(
            [],
            {'company_id': 1},
            [], field_onchange, context=context)

        print('--value-')
        for r, v in res['value'].items():
            print(r, v)
        print('--domain-')
        for r, v in res['domain'].items():
            print(r, v)
        print('--fields-')
        for r, v in res['fields'].items():
            if v['type'] == 'many2one':
                print(r, v)

    def test_search(self):
        api.env['account.move'].search([])

        # print('sech ok,', self.page.store.dataList)


accountMove = AccountMove()
accountMoveLine = AccountMoveLine()
